package data;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

/**
 * data
 *
 * @author Jared Moore
 * @date 10/19/13
 */
public class Town implements Serializable {
	public static final HashMap<Integer, Image> TOWN_IMAGES = new HashMap<>();
	private Map map;

	static {
		try {
			TOWN_IMAGES.put(0, ImageIO.read(new File("res/town/shopTemp.png")));
			TOWN_IMAGES.put(1, ImageIO.read(new File("res/town/land.png")));
			TOWN_IMAGES.put(2, ImageIO.read(new File("res/town/pub.png")));
			TOWN_IMAGES.put(3, ImageIO.read(new File("res/town/exit.jpg")));
			TOWN_IMAGES.put(4, ImageIO.read(new File("res/town/floor.jpeg")));
		} catch (IOException e) {
			System.err.println("A town image is missing");
			System.exit(1);
		}
	}

	public Town(Map map) {
		int[][] playerLocation = new int[2][4];
		playerLocation[1][3] = 1;
		this.map = map;
	}

	/**
	 * Draws the town
	 *
	 * @param g              The graphics element to do the drawing
	 * @param width          The width of the window
	 * @param height         The height of the window
	 * @param playerLocation The location of the player
	 *                       <p/>
	 *                       Player location is set up like
	 *                       1 3 5 7
	 *                       2 4 6 8
	 */
	public void draw(Graphics g, int width, int height, int playerLocation) {
		//first half
		g.drawImage(TOWN_IMAGES.get(0), 0, 0, width / 4, height / 2, null);
		g.drawImage(TOWN_IMAGES.get(1), width / 4, 0, width / 4, height / 2, null);
		g.drawImage(TOWN_IMAGES.get(2), width / 2, 0, width / 4, height / 2, null);
		g.drawImage(TOWN_IMAGES.get(3), 3 * width / 4, 0, width / 4, height / 2, null);

		//bottom half
		g.drawImage(TOWN_IMAGES.get(4), 0, height / 2, width / 4, height / 2, null);
		g.drawImage(TOWN_IMAGES.get(4), width / 4, height / 2, width / 4, height / 2, null);
		g.drawImage(TOWN_IMAGES.get(4), width / 2, height / 2, width / 4, height / 2, null);
		g.drawImage(TOWN_IMAGES.get(4), 3 * width / 4, height / 2, width / 4, height / 2, null);

		//draw player
		int initialHeight = 0;
		if (playerLocation % 2 == 0) {
			initialHeight = height / 2;
		}
		int initialWidth = (((playerLocation + 1) / 2) - 1) * width / 4;
		g.drawImage(map.getCurrentPlayer().getIcon(), initialWidth, initialHeight, width / 4, height / 4, null);
	}
}
